<template>
	<view class="container">
		<u-gap :bg-color="bgColor" :height="height"></u-gap>
		<view class="time-list">
			<view class="item u-flex u-row-between u-col-top u-p-t-50" v-show="isShowAdd">
				<view class="time"><text>今天</text></view>
				<view class="img"><image @click="add" :src="uploadImg" mode="aspectFill"></image></view>
				<view class="content u-m-r-20 u-line-3" style="visibility: hidden;"></view>
			</view>
			<view v-for="(item, index) in list" :key="index" @click="detail(item.id)" class="item u-flex u-row-between u-col-top u-p-t-50">
				<view class="time" v-html="getFormatDate(item.createTime)"></view>
				<view class="img"><image :src="getFirstImg(item.imgUrl)" mode="aspectFill"></image></view>
				<view class="content u-m-r-20 u-line-3">{{ item.article }}</view>
			</view>
			<u-loadmore class="u-m-t-40" :status="loadStatus[0]" bgColor="#fff"></u-loadmore>
		</view>
		<view class="foot-menu u-flex u-row-around">
			<view @click="showuToast">信息大厅</view>
			<view url="">我的动态</view>
			<view @click="myHome">我的主页</view>
			<view @click="toUrl('../notepad/communication/rewardscheme/index')">悬赏计划</view>
		</view>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
export default {
	data() {
		return {
			isShowAdd:false,
			id:'',
			height: 20,
			bgColor: this.$u.color.bgColor,
			marginTop: 30,
			marginBottom: 30,
			baseUrl: this.$mConfig.baseUrl,
			uploadImg: this.$mAssetsPath.uploadImg,
			list: [],
			loadStatus: 'loadmore',
			reqBody:{
				userId:this.$store.state.userInfo.id,
				pageNum:1,
				pageSize:10,
			}
		};
	},
	onLoad(options) {		
		if(options.id){
			this.id = options.id;
			this.reqBody.userId = options.id;
		}else{
			this.isShowAdd = true;
			console.log("userInfo",this.$store.state.userInfo);
			this.id = this.$store.state.userInfo.id;
		}
	},
	onShow(){
		this.initPage();
	},
	onPullDownRefresh: function() {
		this.reqBody.pageNum = 1;
		this.initPage();
		setTimeout(() => {
			uni.stopPullDownRefresh();
		}, 1000);
	},
	onReachBottom:function(e){
		// 此tab为空数据
		this.reqBody.pageNum += 1;
		this.loadStatus= "loading";
		this.initPage(this.reqBody.pageNum);
		setTimeout(() => {
			this.loadStatus="loadmore";
		}, 1000);
	},
	methods: {
		async myHome(){
			if(uni.getStorageSync('token')){
			let result = await this.$apis.fetchUserReviewed({id:this.id});
			if(result.data){
				uni.navigateTo({
					url:"../authorization/homepage?id="+this.id
				})
			}else{
				uni.showToast({
					title:"该成员未完善信息",
					icon:'none'
				})
			}
			}else{
				uni.showToast({
					title:"请先登录",
					icon:'none'
				})
			}
		},
		async initPage(page){
			let result = await this.$apis.workCircleByUserid(this.reqBody);
			if(page){
				this.list = this.list.concat(result.data);
			}else{
				this.list = result.data;
			}
		},
		toUrl(url){
			console.log("reqBody",this.reqBody);
			if(this.reqBody.userId != this.$store.state.userInfo.id){
				uni.showToast({
					title:"暂无权限",
					icon:'none'
				})
				return;
			}
			uni.navigateTo({
				url:url
			})
		},
		detail(id){
			this.$mRouter.push({
				route:this.$mRoutesConfig.releaseNumberDetail,
				query:{
					id:id
				}
			});
		},
		add(){
			this.$mRouter.push({
				route:this.$mRoutesConfig.releaseNumberAdd
			});
		},
		getFormatDate(val) {
			var date = new Date();
			var base = Date.parse(date); // 转换为时间戳
			var year = date.getFullYear(); //获取当前年份
			var mon = date.getMonth() + 1; //获取当前月份
			var day = date.getDate(); //获取当前日
			console.log(year + '-' + mon + '-' + day);

			var timearr = val
				.replace(' ', ':')
				.replace(/\:/g, '-')
				.split('-');
			var timestr = "<text style='font-size: 25px;'>" + timearr[2] + '</text>' + Number(timearr[1]) + '月';

			if (year == Number(timearr[0]) && mon == Number(timearr[1]) && day == Number(timearr[2])) {
				return "<text style='font-size: 25px;'>今天</text>";
			} else if (year == Number(timearr[0]) && mon == Number(timearr[1]) && Number(day - 1) == Number(timearr[2])) {
				return "<text style='font-size: 25px;'>昨天</text>";
			} else {
				return timestr;
			}
		},
		getFirstImg(imgs){
			if(imgs){
				if(imgs.indexOf(',') == -1){
					return this.baseUrl + imgs;
				}else{
					var img = imgs.split(',');
					return this.baseUrl + img[0];
				}
			}
		},
		
		showuToast(){
			this.$refs.uToast.show({
				title: '正在开发，敬请期待',
				type: 'success',
			})
		}
	}
};
</script>

<style scoped lang="scss">
page,.container {background-color: #fff;}
.container {padding-bottom:150rpx;}
.time-list {
	.item {
		.time {
			width: 144rpx;
			text-align: center;
			font-size: 22rpx;
			text {
				font-size: 50rpx;
			}
		}
		.img {
			width: 150rpx;
			height: 150rpx;
			overflow: hidden;
			image {
				width: 150rpx;
				height: 150rpx;
			}
		}
		.content {
			width: 400rpx;
			line-height: 42rpx;
		}
	}
}
.foot-menu {
	position: fixed;
	bottom: 0;
	z-index: 1;
	padding:16rpx 0;
	background: #e6e6eb;
	padding-right: 23rpx;
	view {
		padding: 14rpx 25rpx;
		margin-left:23rpx;
		text-align: center;
		background-color: #fff;
	}
}
</style>
